package com.itda.controller;

import com.itda.annotation.Log;
import com.itda.pojo.Dept;
import com.itda.pojo.Result;
import com.itda.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: itda
 * @Date: 2024/4/27 18:54
 * @Description: 部门管理
 */
//延迟初始化IOC对象
//@Lazy
//非单例 每一次调用getBean都会拿到一个全新的Bean对象
@Scope("prototype")
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    //@Slf4j 等同于以下代码
    //private static Logger log = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;

    //@RequestMapping(value = "/depts",method = RequestMethod.GET) 等同 @GetMapping

    /**
     * 查询部门数据
     * @return
     */
    @GetMapping
    public Result list() {
        log.info("查询全部部门数据");

        List<Dept> deptList = deptService.list();

        return Result.success(deptList);
    }

    /**
     * 删除部门数据
     * @param id
     * @return
     */
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除部门:{}",id);

        deptService.delete(id);

        return Result.success();
    }

    /**
     * 新增部门
     * @return
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门:{}",dept);

        deptService.add(dept);

        return Result.success();
    }

    /**
     * 根据id查询部门
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result query(@PathVariable Integer id){
        log.info("查询部门:{}",id);

        Dept dept = deptService.query(id);

        return Result.success(dept);
    }

    /**
     * 修改部门
     * @return
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门:{}",dept);

        deptService.update(dept);

        return Result.success();
    }
}
